﻿using Abp.Application.Services.Dto;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using Abp.Application.Services;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Collections.Extensions;
using Abp;
using ARchGL.Platform.Utils;
using ARchGL.Platform.Dto;

namespace ARchGL.Platform.Electrocar.Application
{
    public static class BaseServices
    {
        #region IQueryable

        public static async Task<IQueryable<T>> PageByAsync<T>(this IQueryable<T> query, int skipCount, int maxResultCount)
        {
            return await ToPaged(query, skipCount, maxResultCount);
        }

        public static async Task<IQueryable<T>> PageByAsync<T>(this IQueryable<T> query, PagedInputDto paged)
        {
            return await ToPaged(query, paged.SkipCount, paged.MaxResultCount);
        }

        public static async Task<IQueryable<T>> PageByAsync<T>(this IQueryable<T> query, PagedAndSortedResultRequestDto paged)
        {
            return await ToPaged(query, paged.SkipCount, paged.MaxResultCount);
        }

        public static async Task<IQueryable<T>> PageByAsync<T>(this IQueryable<T> query, PagedResultRequestDto paged)
        {
            return await ToPaged(query, paged.SkipCount, paged.MaxResultCount);
        }

        #endregion

        #region IEnumerable

        public static async Task<IEnumerable<T>> PageByAsync<T>(this IEnumerable<T> query, int skipCount, int maxResultCount)
        {
            return await ToPaged(query, skipCount, maxResultCount);
        }

        public static async Task<IEnumerable<T>> PageByAsync<T>(this IEnumerable<T> query, PagedInputDto paged)
        {
            return await ToPaged(query, paged.SkipCount, paged.MaxResultCount);
        }

        public static async Task<IEnumerable<T>> PageByAsync<T>(this IEnumerable<T> query, PagedAndSortedResultRequestDto paged)
        {
            return await ToPaged(query, paged.SkipCount, paged.MaxResultCount);
        }

        #endregion

        private static async Task<IQueryable<T>> ToPaged<T>(IQueryable<T> query, int skipCount, int maxResultCount)
        {
            if (skipCount > 1)
                query = query.Skip((skipCount - 1) * maxResultCount);

            if (maxResultCount > 0)
                query = query.Take(maxResultCount);

            return query;
        }

        private static async Task<IEnumerable<T>> ToPaged<T>(IEnumerable<T> query, int skipCount, int maxResultCount)
        {
            if (skipCount > 1)
                query = query.Skip((skipCount - 1) * maxResultCount);

            if (maxResultCount > 0)
                query = query.Take(maxResultCount);

            return query;
        }
    }

    public class asdfasdf<T> : ApplicationService
    {
        private IEnumerable<T> query;

        public asdfasdf(IEnumerable<T> _query)
        {
            query = _query;
        }

        //public async IReadOnlyList<T> asdfasdf111()
        //{
        //    //return new PagedResultDto<T>(totalCount,
        //    //ObjectMapper.Map<List<T>>(await query.ToListAsync()));

        //    return ObjectMapper.Map<IReadOnlyList<T>>(query.ToList());
        //}
    }
}
